package cst.a703xmq.zyfypt_703xmq.Activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import cst.a703xmq.zyfypt_703xmq.Bean.LoginBean;
import cst.a703xmq.zyfypt_703xmq.R;
import cst.a703xmq.zyfypt_703xmq.Model.ResModel;
import cst.a703xmq.zyfypt_703xmq.Interface.XmqListener;

public class LoginActivity extends AppCompatActivity {
    private EditText user_name;
    private EditText password;
    private Button btn_login;
    private Button btn_reg;
    private ResModel model;
    private XmqListener<LoginBean> listener;
    private SharedPreferences sp;
    private SharedPreferences.Editor editor;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        sp=getSharedPreferences("login",MODE_PRIVATE);

        user_name = (EditText)findViewById(R.id.login_user_name);
        password = (EditText)findViewById(R.id.login_user_password);
        btn_login = (Button)findViewById(R.id.btn_login);
        btn_reg = (Button)findViewById(R.id.btn_reg);
        listener = new XmqListener<LoginBean>() {
            @Override
            public void onResponse(LoginBean loginBean) {
                if(loginBean.getUsername()!=null)
                {
                    String sessionid = loginBean.getSessionid();

                    editor = sp.edit();
                    editor.putString("username",loginBean.getUsername());
                    editor.putString("password",password.getText().toString());
                    editor.putString("sesionid",loginBean.getSessionid());
                    editor.commit();

                    Intent intent = new Intent(LoginActivity.this,
                            ListActivity.class);
                    startActivity(intent);

                }
                else
                {
                    Toast.makeText(LoginActivity.this,"用户名或密码错误",
                            Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFail(String msg) {
                Toast.makeText(LoginActivity.this,msg,
                        Toast.LENGTH_SHORT).show();
            }
        };
        btn_login.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
                model = new ResModel("gson");
                model.login(user_name.getText().toString(),
                        password.getText().toString(),
                        listener);
            }
        });
        btn_reg.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
                Intent intent = new Intent(LoginActivity.this,ResActivity.class);
                startActivity(intent);
            }
        });
    }
}
